
<?php
header("Access-Control-Allow-Origin: '*'");
header("Access-Control-Allow-Methods: '*'");
header("Access-Control-Max-Age, '1728000'");
header("Access-Control-Allow-Headers, 'x-requested-with'");
?> 



<?php

include_once('databaseconnect.php');
$con = databaseconnect();

mysql_select_db("p3db", $con);

$agiid = trim($_REQUEST["agi"]);

$sql_search_agi = "
SELECT * FROM peptide WHERE protein IN
(
SELECT p3dbId FROM idMapping WHERE otherId = UCASE('$agiid')
)
";

$searchresult = mysql_query($sql_search_agi);
$searchresultrows = mysql_num_rows($searchresult);

$response = "{";
$searchidstr = "\"agi\" : \"$agiid\",";
$peptidesstr = " \"peptides\" :";

$orthologsstr = " \"orthologs\": [";
										 
$organismid="";
$organismname="";
$seqstrblock="";
$orthologsblock="";
$orthologsblocks="";

if($searchresultrows==0)
{

$seqstrblock = $seqstrblock."[]";
$orthologsblock = $orthologsblock."{}";
$orthologsblocks = $orthologsblocks.$orthologsblock;
}
else
{
	$seqarray=array();
	while($row = mysql_fetch_array($searchresult))
	{
		array_push($seqarray,$row['sequence']);
	}
	$seqstrblock = join("\",\"",$seqarray);
	$seqstrblock = "[\"".$seqstrblock."\"]";

	$orthologsblock="{";
	$organismid = "3847";
	$organismidstr = "\"organism\":".$organismid.",";
	
	$organismname = "\"glycine max\"";
	$organismnamestr = "\"name\":".$organismname.",";
	
	$subpeptidesstr = " \"peptides\" :";
	$subseqstrblock ="";
	$subseqstrblock = $seqstrblock;
	$subpeptidesstr = $subpeptidesstr.$subseqstrblock;
	$subpeptidesstr = $subpeptidesstr;
	
	$orthologsblock=$orthologsblock.$organismidstr.$organismnamestr.$subpeptidesstr."}";
	
	$orthologsblocks = $orthologsblocks.$orthologsblock;
	$orthologsblock = str_replace("3847","3708",$orthologsblock);
	$orthologsblock = str_replace("\"glycine max\"","\"brassica napus\"",$orthologsblock);
	$orthologsblocks = $orthologsblocks.",".$orthologsblock;
	$orthologsblock = str_replace("3708","3880",$orthologsblock);
	$orthologsblock = str_replace("\"brassica napus\"","\"medicago truncatula\"",$orthologsblock);
	$orthologsblocks = $orthologsblocks.",".$orthologsblock;
	$orthologsblock = str_replace("3880","4530",$orthologsblock);
	$orthologsblock = str_replace("\"medicago truncatula\"","\"oryza sativa\"",$orthologsblock);
	$orthologsblocks = $orthologsblocks.",".$orthologsblock;
	$orthologsblock = str_replace("4530","3702",$orthologsblock);
	$orthologsblock = str_replace("\"oryza sativa\"","\"arabidopsis thaliana\"",$orthologsblock);
	$orthologsblocks = $orthologsblocks.",".$orthologsblock;
	
}

$peptidesstr = $peptidesstr.$seqstrblock;
$peptidesstr = $peptidesstr.",";
$orthologsstr = $orthologsstr.$orthologsblocks;
$orthologsstr = $orthologsstr."]";

$response = $response.$searchidstr.$peptidesstr.$orthologsstr."}";


echo "$response";

mysql_close($con);
?>
